package com.tbs.web.security.impl;

import com.tbs.common.error.AssertUtil;
import com.tbs.common.util.base.StrUtil;
import com.tbs.web.exception.SimpleAuthException;
import com.tbs.web.security.authentication.center.IAuthenticationCenter;
import com.tbs.web.security.authentication.model.DefaultTokenAuthentication;
import com.tbs.web.security.authentication.model.ITokenAuthentication;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import javax.annotation.Resource;
import java.util.function.Supplier;

/**
 * 令牌认证提供者
 *
 * @author tongj
 */
public class TokenAuthenticationProvider implements org.springframework.security.authentication.AuthenticationProvider {

    @Resource
    private IAuthenticationCenter authCenter;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        AssertUtil.notNull(authentication, "未获取到有效的Authentication");
        ITokenAuthentication tokenAuthentication = (ITokenAuthentication)authentication;
        AssertUtil.isTrue(StrUtil.isNotEmpty(tokenAuthentication.getErrorMessage()), new Supplier<RuntimeException>() {
            @Override
            public RuntimeException get() {
                return new SimpleAuthException(tokenAuthentication.getErrorMessage());
            }
        });

        return authCenter.authenticate(tokenAuthentication);
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication == DefaultTokenAuthentication.class;
    }
}
